package com.five.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.five.admin.server.CategoryService;
import com.five.core.result.Result;
import com.five.core.result.ResultEnum;
import com.five.pojo.dto.BatchIds;
import com.five.pojo.entity.Category;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;
    @RequestMapping(value = "/queryCategoryTree", method = RequestMethod.GET)
    public Result queryCategoryTree(){
        return Result.buildSuccess(categoryService.queryCategoryTree());
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Category category){
        category.setCreateTime(LocalDateTime.now());
        return categoryService.save(category) ? Result.buildSuccess() : Result.buildFail(ResultEnum.ADD_FALSE);
    }

    @RequestMapping(value = "/queryOne", method = RequestMethod.GET)
    public Result queryOne(Long id){
        return Result.buildSuccess(categoryService.getById(id));
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result update(@RequestBody Category category){
        return categoryService.updateById(category) ? Result.buildSuccess() : Result.buildFail(ResultEnum.UPDATE_FALSE);
    }

    /**
     * 删除分类
     * 1.判断该分类下是否有子类
     * 2.判断该分类下是否有产品
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result delete(Long id){
        boolean deleted = categoryService.deleteCategory(id);
        return deleted?Result.buildSuccess():Result.buildFail("存在子类或者存在产品，，暂时不能删除");
    }

    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    public Result deleteBatch(@RequestBody BatchIds batchIds){
        String string = categoryService.deleteCategoryBatch(batchIds.getIds());
        return string.equals("id:")?Result.buildSuccess():Result.buildFail(string);
    }

    @RequestMapping(value = "/queryAllOne", method = RequestMethod.GET)
    public Result queryAllOne(){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, 0);
        return Result.buildSuccess(categoryService.list(queryWrapper));
    }

    @RequestMapping(value = "/queryAllTwo", method = RequestMethod.GET)
    public Result queryAllTwo(){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Category::getParentId, 0);
        return Result.buildSuccess(categoryService.list(queryWrapper));
    }

}
